package com.bytedance.common.plugin;

import android.text.TextUtils;

import com.bytedance.common.plugin.framework.model.DeletePluginItem;
import com.bytedance.common.plugin.framework.model.InstallPluginItem;
import com.bytedance.common.plugin.framework.model.ProcessType;
import com.bytedance.common.plugin.framework.model.ResourceType;
import com.bytedance.common.utility.Logger;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class a {
    public static final class a$a {
        public String packageName;
        public String proxyClass;
        public ResourceType resourceType;
        public ProcessType processType;

        public a$a(String packageName, String proxyClass, ResourceType resourceType, ProcessType processType) {
            this.packageName = packageName;
            this.proxyClass = proxyClass;
            this.resourceType = resourceType;
            this.processType = processType;
        }
    }

    public static final com.bytedance.common.plugin.framework.util.a<String, String> a = new com.bytedance.common.plugin.framework.util.a();
    public static final com.bytedance.common.plugin.framework.util.a<String, String> b = new com.bytedance.common.plugin.framework.util.a(a.b, a.a);
    ;
    public static final Map c = new HashMap();
    public static final Map d = new HashMap();
    public static final Set<InstallPluginItem> installPluginItems = new HashSet<>();
    private static PluginDepend sPluginDepend;

    public static InstallPluginItem getInstallPluginItem(String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return null;
        } else {
            Iterator<InstallPluginItem> iterator = installPluginItems.iterator();
            while (iterator.hasNext()) {
                InstallPluginItem pluginItem = iterator.next();
                if (pluginItem == null) {
                    continue;
                }

                if (!packageName.equals(pluginItem.packageName)) {
                    continue;
                }

                return pluginItem;
            }
            return null;
        }
    }

    public static void updatePluginItems() {
        List<InstallPluginItem> pluginItemList = com.bytedance.common.plugin.framework.update.PluginUpdateHelper.getUpdatePluginItemList();
        if (pluginItemList != null) {
            Iterator<InstallPluginItem> iterator = pluginItemList.iterator();
            while (iterator.hasNext()) {
                InstallPluginItem pluginItem = iterator.next();
                if (Logger.debug()) {
                    Logger.d("Plugin", "updatePluginItems " + pluginItem.packageName);
                }

                if (installPluginItems.contains(pluginItem)) {
                    installPluginItems.remove(pluginItem);
                }
                installPluginItems.add(pluginItem);
            }
        }

        List<DeletePluginItem> pluginItems = com.bytedance.common.plugin.framework.update.PluginUpdateHelper.getDeletePluginItemList();
        if (pluginItems != null) {
            Iterator<DeletePluginItem> iterator = pluginItems.iterator();
            while (iterator.hasNext()) {
                DeletePluginItem pluginItem = iterator.next();
                if (Logger.debug()) {
                    Logger.d("Plugin", "deletePluginItems " + pluginItem.packageName);
                }

                if (installPluginItems.contains(pluginItem)) {
                    installPluginItems.remove(pluginItem);
                }
            }
        }
    }

    public static void setPluginDepend(PluginDepend pluginDepend) {
        sPluginDepend = pluginDepend;
    }

    public static PluginDepend getPluginDepend() {
        if (sPluginDepend == null) {
            throw new IllegalArgumentException("sPluginDepend is not init !!!");
        }
        return sPluginDepend;
    }

    public static boolean b(String packageName) {
        return TextUtils.isEmpty(packageName) ? false : d.containsKey(packageName);
    }
}

